package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyRegularAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.TenderDetailData;
import com.kkd.kkdapp.http.entity.TenderDetailMapList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.utils.UIEmptyHandleUtil;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

/****
 * 
 * @Date 2016-3-15
 * 
 * @author mty
 * 
 * @Name 我的定期
 * 
 */
public class MyRegularActivity extends BaseActivity implements OnClickListener,
		KKDScrollListener {

	private final int CODE_ZERO = 1;
	private final int CODE_ELSE = 2;

	private DecimalFormat df = new DecimalFormat("###,##0.01");
	private RelativeLayout condition_title; // 返回
	private RelativeLayout history_tv; // 历史
	private long pageNO = 1;
	private long pageSize = 10;
	private KkdXlistView manage_lv;
	private ListView mListView;
	private ImageView mNullImg;
	private TextView mNullTv;
	private List<TenderDetailMapList> mapList = new ArrayList<TenderDetailMapList>();
	private MyRegularAdapter adapter;
	private long pageCount;
	private boolean REFRESHFLAG = true;
	private long dpAmuout;
	private long frozenAssets;
	private TenderDetailData data;
	private long interest;
	private TextView total_tv;
	private TextView frozen_tv;
	private RelativeLayout total_popup, frozen_popup;
	private PopupWindow mDialog, estimateDialog;
	private ImageView icon1, icon2;
	private TextView investment_bt;
	private LinearLayout investment_ll;
	private boolean firstEntryFlag = true;
	private RelativeLayout rl_null;

	private Handler mHandler = new Handler() {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.Handler#handleMessage(android.os.Message)
		 */
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				total_tv.setText(df.format(dpAmuout * 0.01));
				frozen_tv.setText(df.format(interest * 0.01));
				if (adapter == null) {
					adapter = new MyRegularAdapter(MyRegularActivity.this,
							mapList);
					mListView.setAdapter(adapter);
				}
				long PageSize = data.getPageSize();
				long totalCount = data.getTotalCount();
				if (totalCount % PageSize != 0) {
					pageCount = (totalCount / PageSize) + 1;
				} else {
					pageCount = (totalCount / PageSize);
				}
				if (pageNO > pageCount) {
					manage_lv.stopLoadMore(-1);
					if (REFRESHFLAG) {
						manage_lv.stopLoadMore(1);
					}
				} else {
					manage_lv.setNoMore(false);
					if (pageNO == 1) {
						mapList.clear();
					}
					mapList.addAll(data.getMapList());
					adapter.notifyDataSetChanged();
					if (data.getMapList().size() < pageSize) {
						manage_lv.stopLoadMore(1);
					} else {
						manage_lv.stopLoadMore(0);
					}
				}
				manage_lv.stopRefresh();
				manage_lv.initState();
				Time_Date time_Date = new Time_Date();
				String time = time_Date.get_Year_Day();
				manage_lv.setRefreshTime(time);
				firstEntryFlag = true;
				if (REFRESHFLAG) {
					if (mapList.size() == 0) {
						firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
								MyRegularActivity.this, firstEntryFlag, true,
								rl_null, mNullImg, mNullTv, "您尚未投资任何定期标",
								R.drawable.img_null);
						investment_ll.setVisibility(View.VISIBLE);
					} else {
						firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
								MyRegularActivity.this, firstEntryFlag, false,
								rl_null, mNullImg, mNullTv, "", 0);
						investment_ll.setVisibility(View.GONE);
					}
				}
				break;
			case CODE_ELSE:
				manage_lv.stopRefresh();
				manage_lv.initState();
				manage_lv.stopLoadMore(1);
				int statusCode = (Integer) msg.obj;
				if (statusCode == KkdHttpClient.NO_NET) {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							MyRegularActivity.this, firstEntryFlag, true,
							rl_null, mNullImg, mNullTv, "网络错误，请稍后重试",
							R.drawable.img_null_net);
				} else if (statusCode == KkdHttpClient.RESOLUTION_EXCEPTION) {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							MyRegularActivity.this, firstEntryFlag, true,
							rl_null, mNullImg, mNullTv, "服务异常，请稍后重试",
							R.drawable.img_null_net);
				}
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_regular);
		initView();
		if (PreferenceUtils.getLogin(this) == 0) {
			requestTenderDetail();
		} else {
			Intent intent = new Intent(this, LoginActivity.class);
			startActivityForResult(intent, LoginActivity.TO_LOGIN);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	/*************
	 * 初始化控件
	 *************/
	public void initView() {
		condition_title = (RelativeLayout) this
				.findViewById(R.id.condition_title);
		history_tv = (RelativeLayout) this.findViewById(R.id.history_tv);
		mNullImg = (ImageView) this.findViewById(R.id.id_no_content);
		mNullTv = (TextView) this.findViewById(R.id.id_no_content_text);
		manage_lv = (KkdXlistView) this.findViewById(R.id.manage_lv);
		manage_lv.setKKDScrollListener(this);
		condition_title.setOnClickListener(this);
		history_tv.setOnClickListener(this);
		mListView = manage_lv.mListView;
		mListView.setDividerHeight(0);
		manage_lv.startRefresh();

		View view = LayoutInflater.from(this).inflate(
				R.layout.myregular_one_item, null);
		total_tv = (TextView) view.findViewById(R.id.total_tv);
		frozen_tv = (TextView) view.findViewById(R.id.frozen_tv);
		total_popup = (RelativeLayout) view.findViewById(R.id.total_popup);
		frozen_popup = (RelativeLayout) view.findViewById(R.id.frozen_popup);
		icon1 = (ImageView) view.findViewById(R.id.icon1);
		icon2 = (ImageView) view.findViewById(R.id.icon2);
		mListView.addHeaderView(view);
		total_popup.setOnClickListener(this);
		frozen_popup.setOnClickListener(this);
		investment_bt = (TextView) this.findViewById(R.id.investment_bt);
		investment_bt.setOnClickListener(this);
		investment_ll = (LinearLayout) this.findViewById(R.id.investment_ll);
		rl_null = (RelativeLayout) this.findViewById(R.id.rl_null);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.condition_title: // 返回
			finish();
			break;
		case R.id.history_tv: // 　历史
			SystemUtils.onEvent("Account_Regular_History", 1);
			Intent intent = new Intent(this, HistoryActivity.class);
			startActivity(intent);
			break;
		case R.id.total_popup:
			LogUtil.e("TAG", "-------------");
			LogUtil.e("icon1X", "" + icon1.getX());
			LogUtil.e("icon1Y", "" + icon1.getY());
			showmDialog(icon1, "您定期投标成功的金额(不含快快享福),包括冻结资产", icon1.getX());
			break;
		case R.id.frozen_popup:
			LogUtil.e("TAG", "=============");
			LogUtil.e("icon2X", "" + icon2.getX());
			LogUtil.e("icon2Y", "" + icon2.getY());
			showmDialog1(icon2, "您定期投标未到账的预期收益(不包含快快享福预期收益)", icon2.getX());
			break;
		case R.id.investment_bt:
			startActivity(new Intent(this, HomeActivity.class));
			HomeActivity.mActivity.homeNavigation();
			finish();
			break;
		}
	}

	/**
	 * 请求接口
	 */
	public void requestTenderDetail() {
		KkdHttpMagager.getInstance().getTenderDetailManager("5002,5003,5004",
				pageNO, pageSize, TenderDetailData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						data = (TenderDetailData) responseBody;
						dpAmuout = data.getDqAmount();
						frozenAssets = data.getFrozenAssets();
						interest = data.getInterest();
						mHandler.sendEmptyMessage(CODE_ZERO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.obj = statusCode;
						msg.what = CODE_ELSE;
						mHandler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		requestTenderDetail();
		REFRESHFLAG = true;
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		requestTenderDetail();
		REFRESHFLAG = false;
	}

	public void showmDialog(ImageView img, String describe, float coordinate) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		mDialog = DialogUtils.bubblePupopwindow2(this, describe, coordinate);
		mDialog.showAsDropDown(img);
	}

	public void showmDialog1(ImageView img, String describe, float coordinate) {
		if (estimateDialog != null) {
			estimateDialog.dismiss();
			estimateDialog = null;
		}
		estimateDialog = DialogUtils.bubblePupopwindow3(this, describe,
				coordinate);
		estimateDialog.showAsDropDown(img);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == LoginActivity.TO_LOGIN) {
			if (resultCode == LoginActivity.LOGIN_CANCLE) {
				finish();
			}
		}
	}
}
